package icu.makka.yygh.controller.api;

import icu.makka.yygh.common.exception.CustomException;
import icu.makka.yygh.common.helper.HttpRequestHelper;
import icu.makka.yygh.common.utils.MD5;
import icu.makka.yygh.model.hosp.Department;
import icu.makka.yygh.model.hosp.Hospital;
import icu.makka.yygh.model.hosp.Schedule;
import icu.makka.yygh.result.Result;
import icu.makka.yygh.result.ResultCodeEnum;
import icu.makka.yygh.service.DepartmentService;
import icu.makka.yygh.service.HospitalService;
import icu.makka.yygh.service.HospitalSetService;
import icu.makka.yygh.service.ScheduleService;
import icu.makka.yygh.service.impl.DepartmentServiceImpl;
import icu.makka.yygh.service.impl.HospitalServiceImpl;
import icu.makka.yygh.service.impl.HospitalSetServiceImpl;
import icu.makka.yygh.service.impl.ScheduleServiceImpl;
import icu.makka.yygh.vo.hosp.DepartmentQueryVo;
import icu.makka.yygh.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Slf4j
@Api(tags = "医院管理API接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalServiceImpl hospitalService;

    @Autowired
    private HospitalSetServiceImpl hospitalSetService;

    @Autowired
    private DepartmentServiceImpl departmentService;

    @Autowired
    private ScheduleServiceImpl scheduleService;

    /**
     * 排班删除接口
     * @param request
     * @return
     */
    @ApiOperation(value = "排班删除接口")
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request) {
        log.info("排班删除接口...");

        //获取医院传递过来的信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
        //获取医院传递过来的签名
        String sign = (String)paramMap.get("sign");
        //根据传递过来的医院编码，查询数据库，查询签名
        String hoscode = (String) paramMap.get("hoscode");
        //如果编码不存在，抛出异常
        if (StringUtils.isEmpty(hoscode)) {
            throw new CustomException(ResultCodeEnum.PARAM_ERROR);
        }
        String signKey = hospitalSetService.getSignByHosCode(hoscode);
        //将数据库查询出来的签名进行MD5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        //验证签名是否相等
        if (!sign.equals(signKeyMD5)) {
            //如果不相等，抛出异常
            throw new CustomException(ResultCodeEnum.SIGN_ERROR);
        }
        //获取医院排班编号
        String hosScheduleId = (String)paramMap.get("hosScheduleId");
        //调用service层
        scheduleService.removeSchedule(hosScheduleId);

        //删除成功，返回结果
        return Result.ok();
    }

    /**
     * 排班新增接口
     * @param request
     * @return
     */
    @ApiOperation(value = "排班新增接口")
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        log.info("排班新增接口...");

        //获取医院传递过来的信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
        //获取医院传递过来的签名
        String sign = (String)paramMap.get("sign");
        //根据传递过来的医院编码，查询数据库，查询签名
        String hoscode = (String) paramMap.get("hoscode");
        //如果编码不存在，抛出异常
        if (StringUtils.isEmpty(hoscode)) {
            throw new CustomException(ResultCodeEnum.PARAM_ERROR);
        }
        String signKey = hospitalSetService.getSignByHosCode(hoscode);
        //将数据库查询出来的签名进行MD5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        //验证签名是否相等
        if (!sign.equals(signKeyMD5)) {
            //如果不相等，抛出异常
            throw new CustomException(ResultCodeEnum.SIGN_ERROR);
        }
        //调用service层
        scheduleService.page(paramMap);

        //新增成功，返回结果
        return Result.ok();
    }

    /**
     * 排班查询接口
     * @param request
     * @return
     */
    @ApiOperation(value = "排班查询接口")
    @PostMapping("schedule/list")
    public Result schedule(HttpServletRequest request) {
        log.info("排班查询接口...");

        //获取医院传递过来的信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
        //获取医院传递过来的签名
        String sign = (String)paramMap.get("sign");
        //根据传递过来的医院编码，查询数据库，查询签名
        String hoscode = (String) paramMap.get("hoscode");
        //如果编码不存在，抛出异常
        if (StringUtils.isEmpty(hoscode)) {
            log.info("医院编号为空...");
            throw new CustomException(ResultCodeEnum.PARAM_ERROR);
        }
        String signKey = hospitalSetService.getSignByHosCode(hoscode);
        //将数据库查询出来的签名进行MD5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        //验证签名是否相等
        if (!sign.equals(signKeyMD5)) {
            //如果不相等，抛出异常
            throw new CustomException(ResultCodeEnum.SIGN_ERROR);
        }
        //获取科室编号
        String depcode = (String)paramMap.get("depcode");

        //构造ScheduleOrderVo对象
        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);
        //获取分页信息
        Integer page = Integer.valueOf((String)paramMap.get("page"));
        Integer limit = Integer.valueOf((String)paramMap.get("limit"));
        //调用service层
        Page<Schedule> pageInfo = scheduleService.page(page, limit, scheduleQueryVo);

        return Result.ok(pageInfo);
    }

    /**
     * 科室删除接口
     * @param request
     * @return
     */
    @ApiOperation(value = "科室删除接口")
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        log.info("删除科室接口...");

        //获取医院传递过来的信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
        //获取医院传递过来的签名
        String sign = (String)paramMap.get("sign");
        //根据传递过来的医院编码，查询数据库，查询签名
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignByHosCode(hoscode);
        //将数据库查询出来的签名进行MD5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        //验证签名是否相等
        if (!sign.equals(signKeyMD5)) {
            //如果不相等，抛出异常
            throw new CustomException(ResultCodeEnum.SIGN_ERROR);
        }
        //获取科室编码
        String depcode = (String) paramMap.get("depcode");
        //如果编码不存在，抛出异常
        if (StringUtils.isEmpty(hoscode) || StringUtils.isEmpty(depcode)) {
            throw new CustomException(ResultCodeEnum.PARAM_ERROR);
        }
        //调用service层
        departmentService.removeDepartment(hoscode, depcode);

        //删除成功，返回结果
        return Result.ok();
    }

    /**
     * 科室查询接口
     * @param request
     * @return
     */
    @ApiOperation(value = "科室查询接口")
    @PostMapping("department/list")
    public Result page(HttpServletRequest request) {
        log.info("科室查询接口...");

        //获取医院传递过来的信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
        //获取医院传递过来的签名
        String sign = (String)paramMap.get("sign");
        //根据传递过来的医院编码，查询数据库，查询签名
        String hoscode = (String) paramMap.get("hoscode");
        //如果编码不存在，抛出异常
        if (StringUtils.isEmpty(hoscode)) {
            throw new CustomException(ResultCodeEnum.PARAM_ERROR);
        }
        //获取科室编码
        String depcode = (String) paramMap.get("depcode");
        String signKey = hospitalSetService.getSignByHosCode(hoscode);
        //将数据库查询出来的签名进行MD5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        //验证签名是否相等
        if (!sign.equals(signKeyMD5)) {
            //如果不相等，抛出异常
            throw new CustomException(ResultCodeEnum.SIGN_ERROR);
        }
        //获取分页参数
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        departmentQueryVo.setDepcode(depcode);
        Integer limit = Integer.valueOf((String)paramMap.get("limit"));
        Integer page = Integer.valueOf((String)paramMap.get("page"));
        //调用service层
        Page<Department> pageInfo = departmentService.page(page, limit, departmentQueryVo);

        //返回结果
        return Result.ok(pageInfo);
    }

    /**
     * 科室上传接口
     * @param request
     * @return
     */
    @ApiOperation(value = "科室上传接口")
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        log.info("科室上传接口...");

        //获取医院传递过来的信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
        //获取医院传递过来的签名
        String sign = (String)paramMap.get("sign");
        //根据传递过来的医院编码，查询数据库，查询签名
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignByHosCode(hoscode);
        //将数据库查询出来的签名进行MD5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        //验证签名是否相等
        if (!sign.equals(signKeyMD5)) {
            //如果不相等，抛出异常
            throw new CustomException(ResultCodeEnum.SIGN_ERROR);
        }
        //调用service层
        departmentService.saveDepartment(paramMap);

        //新增成功，返回结果
        return Result.ok();
    }

    /**
     * 医院查询接口
     * @param request
     * @return
     */
    @ApiOperation(value = "医院查询接口")
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request) {
        log.info("医院查询接口");

        //获取医院传递过来的信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
        //获取医院传递过来的签名
        String sign = (String)paramMap.get("sign");
        //根据传递过来的医院编码，查询数据库，查询签名
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignByHosCode(hoscode);
        //将数据库查询出来的签名进行MD5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        //验证签名是否相等
        if (!sign.equals(signKeyMD5)) {
            //如果不相等，抛出异常
            throw new CustomException(ResultCodeEnum.SIGN_ERROR);
        }
        //调用service层
        Hospital hospital = hospitalService.getHospitalByHoscode(hoscode);

        //查询成功，返回结果
        return Result.ok(hospital);
    }

    /**
     * 医院上传接口
     * @param request
     * @return
     */
    @ApiOperation(value = "医院上传接口")
    @PostMapping("saveHospital")
    public Result saveHosp(HttpServletRequest request) {
        log.info("医院上传接口");

        //获取医院传递过来的信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
        //获取医院传递过来的签名
        String sign = (String)paramMap.get("sign");
        //根据传递过来的医院编码，查询数据库，查询签名
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignByHosCode(hoscode);
        //将数据库查询出来的签名进行MD5加密
        String signKeyMD5 = MD5.encrypt(signKey);
        //验证签名是否相等
        if (!sign.equals(signKeyMD5)) {
            //如果不相等，抛出异常
            throw new CustomException(ResultCodeEnum.SIGN_ERROR);
        }
        //调用service的方法
        hospitalService.save(paramMap);

        return Result.ok();
    }
}
